package com.dayu.finecomm.protocol.pojo;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayu.finecomm.utils.Helpers;

/**
 * IOT上报报文
 * 
 * @author XuJialiang
 * @since 2017-10-18 14:26:12
 * @version V1.0.0
 */
public class IotTPacket extends TPacket {
	static Logger LOGGER = LoggerFactory.getLogger(IotTPacket.class);

	/**
	 * 协议类别
	 * 
	 * @author XuJialiang
	 * @since 2017-10-18 14:33:18
	 * @version V1.0.0
	 */
	public static enum VType {

		nbxb(0), nbdb(1), cmt(2);

		int val;

		private VType(int type) {
			this.val = type;
		}

		public int value() {
			return val;
		}

		@Override
		public String toString() {
			return Integer.toString(val);
		}
	}

	/**
	 * 协议类别
	 */
	public VType type;

	/**
	 * 版本号
	 */
	public String version;

	/**
	 * 设备唯一识别号
	 */
	public String deviceid;

	/**
	 * 平台识别号
	 */
	public String appid;

	/**
	 * 数据时间戳，单位：毫秒
	 */
	public Long timestamp;

	/**
	 * 命令ID
	 */
	public String cmdid;

	public String cmdbody;

	/**
	 * IOT报表生成时间戳
	 */
	public Long created;

	public LinkedList<IotTSensor> sensors;

	public IotTPacket(VType type, String cmdid, String body) {
		this.type = type;
		this.cmdid = cmdid;
		this.cmdbody = body;
		this.created = System.currentTimeMillis();
	}

	public IotTPacket(VType type, String deviceid, Long timestamp) {
		this(type, null, deviceid, null, timestamp);
	}

	public IotTPacket(VType type, String deviceid, String appid, Long timestamp) {
		this(type, null, deviceid, appid, timestamp);
	}

	public IotTPacket(VType type, String version, String deviceid, String appid, Long timestamp) {
		this.type = type;

		if (null == version || version.trim().isEmpty()) {
			this.version = "1.0";
		} else {
			this.version = version;
		}

		if (null == deviceid) {
			this.deviceid = " ";
		} else {
			this.deviceid = deviceid;
		}

		if (null == appid || appid.trim().isEmpty()) {
			this.appid = "0";
		} else {
			this.appid = appid;
		}

		this.timestamp = timestamp;
		this.created = System.currentTimeMillis();

		sensors = new LinkedList<>();
	}

	/**
	 * 生成协议头及设备信息
	 * 
	 * @return
	 */
	private String header() {
		StringBuilder sb = new StringBuilder();

		// 添加固定头，物联网协议iot
		sb.append("iot");
		sb.append(";");
		// 添加协议类型
		sb.append(this.type.name());
		sb.append(";");
		// 添加版本号
		sb.append("ver=");
		sb.append(this.version);
		sb.append(",");
		// 添加设备唯一识别号
		sb.append("deviceid=");
		sb.append(this.deviceid);
		sb.append(",");
		// 添加平台号
		sb.append("app=");
		sb.append(this.appid);
		sb.append(";");

		return sb.toString();
	}

	/**
	 * 生成IOT报文字符串
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		if (VType.cmt.value() == this.type.value()) {
			encodeCMT(sb);
			return sb.toString();
		}

		String head = header();

		sb.append(head);

		boolean first = true;

		for (IotTSensor s : sensors) {
			if (!first) {
				sb.append(',');
			} else {
				first = false;
			}

			sb.append(s.toString());
		}

		sb.append(";");
		sb.append(this.timestamp);
		sb.append("\\r\\n");

		return sb.toString();
	}

	/**
	 * 解析命令下发通知
	 * 
	 * @param sb
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private int encodeCMT(StringBuilder sb) {

		sb.append("CMT");
		sb.append("{");
		sb.append(this.cmdid);
		sb.append("|");

		sb.append(encodeBase64(this.cmdbody));
		sb.append("|");

		Timestamp ts = new Timestamp(this.created);

		String tsStr = "";

		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		tsStr = sdf.format(ts);

		sb.append(tsStr);
		sb.append("}");

		return 0;
	}

	private String encodeBase64(String source) {
		return Helpers.encodeBASE64(source);
	}
}
